# 这个脚本用于将raw文件夹里的剧本文件编译成data文件夹里的数据文件
import time
import struct
import os
import sys

RAW_PATH = 'raw/'
OUT_PUT_FILE = 'main.mzpak'

def show_usage():
	print('usage: python build.py [command]')
	print('command: ')
	print('    0. build(build .mz files)')
	print('    1. pack(pack data files into one file)')


def get_mz_files():
	# 用于整合.mz文件
	tmp = os.listdir(RAW_PATH)
	print(tmp)

#==========================================================
# build 功能
def build():
	print('> start build...')
	get_mz_files()
	print('> build over! OvO Thank You~')
	
# pack打包功能
def pack():
	print(' > start pack')
	now = int(time.time())
	magic = b'\x2e\x4d\x7a\x31'   # '.Mz1'
	print('    > magic: '+magic.decode())
	print('    > timestamp: '+str(now))
	now = struct.pack('<I', now)
	with open(OUT_PUT_FILE, 'wb') as f:
		f.write(magic)
		f.write(now)
		# 打包文件
		# ...
		
	print(' > pack over!!!')
	

if __name__ == '__main__':
	# 检查参数个数
	# print(f'    > 参数个数:{len(sys.argv)}')
	# print(f'    > 参数列表:{str(sys.argv)}')
	
	
	pack()   # 用于测试
	print("\n")
	
	
	if len(sys.argv) <= 1:
		show_usage()
		exit()
	command = sys.argv[1]    # get command
	if command == 'build':
		build()
	elif command == 'pack':
		pack()
	else:
		print('invalid command "{}"!'.format(command))
		show_usage()
	
